package activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;

public class AdjustVoiceAlarmActivity extends HiActivity implements ICameraIOSessionCallback, View.OnClickListener {
    @BindView(R.id.title) TitleView title;
    private MyCamera mMyCamera;
    @BindView(R.id.tv_sensitivity_rate) TextView tv_sensitivity_rate;
    @BindView(R.id.sb_sensitivity) SeekBar sb_sensitivity;
    @BindView(R.id.tv_application) TextView tv_application;
    @BindView(R.id.tv_volume) TextView tv_volume;


    @Override
    protected int setLayoutId() {
        return R.layout.activity_adjust_voice_alarm;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        getIntentData();
        initViewAndData();
        setListenres();

    }

    private void setListenres() {
        sb_sensitivity.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 1 && progress <= 100) {
                    tv_sensitivity_rate.setText(progress + "");
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        tv_application.setOnClickListener(this);
        tv_volume.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mMyCamera != null) {
            mMyCamera.registerIOSessionListener(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mMyCamera != null) {
            mMyCamera.unregisterIOSessionListener(this);
        }
    }

    private void initViewAndData() {
        title.setTitle(getString(R.string.adjust_sound_alarm));
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which){
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        finish();
                        break;
                }
            }
        });
        sb_sensitivity.setMax(100);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            sb_sensitivity.setMin(1);
        }
        tv_volume.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    private void getIntentData() {
       String str_uid=getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
       if(!TextUtils.isEmpty(str_uid)){
           for(MyCamera camera:HiDataValue.CameraList){
               if(str_uid.equals(camera.getUid())){
                   this.mMyCamera=camera;
                   break;
               }
           }
       }
        //发送声音报警
        if (mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_GET_AUDIO_ALM_PARAM)) {
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_AUDIO_ALM_PARAM, new byte[0]);
        }
    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {

    }

    @Override
    public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
        if (arg0 != mMyCamera) return;
        Bundle bundle = new Bundle();
        bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
        Message msg = mHandler.obtainMessage();
        msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        msg.obj = arg0;
        msg.arg1 = arg1;
        msg.arg2 = arg3;
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL:
                    if (msg.arg2 == 0) {//成功
                        handReceiveIOCTRLSuccess(msg);
                    } else {
                        handReceiveIOCTRLFail(msg);
                    }
                    break;

            }

        }
    };

    private void handReceiveIOCTRLFail(Message msg) {
    }

    private void handReceiveIOCTRLSuccess(Message msg) {
        Bundle bundle = msg.getData();
        byte[] data = bundle.getByteArray(HiDataValue.EXTRAS_KEY_DATA);
        switch (msg.arg1){
            case HiChipDefines.HI_P2P_GET_AUDIO_ALM_PARAM:
                voiceParam = new HiChipDefines.HI_P2P_S_ALM_PARAM(data);
                tv_sensitivity_rate.setText(voiceParam.sensi+"");
                sb_sensitivity.setProgress(voiceParam.sensi);
                break;
            case HiChipDefines.HI_P2P_SET_AUDIO_ALM_PARAM:
                dismissjuHuaDialog();
                Intent intent=new Intent();
                intent.putExtra("rate",tv_sensitivity_rate.getText().toString().trim());
                setResult(Activity.RESULT_OK,intent);
                finish();
                break;
        }
    }

    private HiChipDefines.HI_P2P_S_ALM_PARAM voiceParam;



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_application:
                int rate=Integer.parseInt(tv_sensitivity_rate.getText().toString());
                voiceParam.sensi=rate;
                showjuHuaDialog();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_AUDIO_ALM_PARAM, voiceParam.parseContent());
                break;
            case R.id.tv_volume:
                Intent intent=new Intent(AdjustVoiceAlarmActivity.this,AudioSettingActivity.class);
                intent.putExtra(HiDataValue.EXTRAS_KEY_UID,mMyCamera.getUid());
                startActivity(intent);
                break;
        }

    }


}
